/**
 * Created by jocky on 07/10/2017.
 */
$().ready(function () {
    //入金历史
    rechargeObj.history();

    //入金提示
    // var PAY_RECHARGE_SN = getCookie('PAY_RECHARGE_SN');
    // if (PAY_RECHARGE_SN) {
    //     rechargeObj.info();
    // }
});

/**
 * 获取指定的cookie
 *
 * @param name
 * @returns {string}
 */
function getCookie(name) {
    if (document.cookie.length > 0) {
        var start = document.cookie.indexOf(name + "=");
        if (start != -1) {
            start = start + name.length + 1;
            var end = document.cookie.indexOf(";", start);
            if (end == -1) end = document.cookie.length;
            return document.cookie.substring(start, end);
        }
    }

    return ""
}

var rechargeObj = {
    moneyAuth: function () {
        swal({
            title: '请确认汇款金额？',
            text: "提交后需要等待后台的审核，请提交最近的支付成功金额",
            type: 'warning',
            showCancelButton: true,
            cancelButtonColor: '#d33',
            cancelButtonText: '取消',
            confirmButtonColor: '#3085d6',
            confirmButtonText: '确定'
        }).then(function () {
            rechargeObj.moneySubmit();
        }).catch(swal.noop);
    },

    /**
     * 提交
     */
    moneySubmit: function () {
        var form = $("#form_money");
        var params = {};
        var validFlag = true;
        var moneys=document.getElementById("exampleInputMoney").value;
        $.each(form.find("input,select"), function (i, v) {
            if (!v.name) return true;
            var required = $(v).attr('data-required');
                var type = $(v).attr('type');
                if(type=="FILE" ||type=="file" ) {
                        var obj = form.find("input[name=money_order]");
                        var objImg = obj.parent().parent().parent().parent().find('img');
                        var base64Img = objImg[2].src;
                        if (required == 1 && !base64Img) {
                            setNotify($(v).attr('data-error'), true);

                            validFlag = false;
                            return false;
                        }

                        var img = new Image();
                        img.src = base64Img;
                        var width = img.width;
                        var height = img.height;
                        var maxWidthSize = 800;
                        var scale = 1;
                        if (width > maxWidthSize || height > maxWidthSize) {
                            if (width > height) {
                                scale = maxWidthSize / width;
                            } else {
                                scale = maxWidthSize / height;
                            }
                        }
                        if (scale == 1) {
                            params[v.name] = base64Img;
                        } else {
                            var canvas = document.createElement("canvas");
                            var ctx = canvas.getContext("2d");

                            canvas.width = scale * width;
                            canvas.height = scale * height;
                            ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
                            var nData = canvas.toDataURL('image/jpeg');
                            params[v.name] = nData;
                        }
                }
        });

        if (!validFlag) {
            return false;
        }
        params['moneys']=moneys;
        $.post("/money/recharge-submit", params, function (json) {
            if (json.code == 1) {
                setNotify(json.msg, true);
                window.location.reload();
            } else {
                //跳转到支付页面
                // window.location.href = '/interface#/money/recharge';
                window.location.reload();
            }
        }, 'json');
    },

    /**
     * 入金提醒
     */
    info: function () {
        $.getJSON('/money/recharge-pay-info', function (json) {
            var status = json.status;
            var msg = "";
            if (status != 2) {
                msg = '入金失败 | ' + json.code;
            } else {
                msg = '入金成功';
            }

            var obj = {
                title: '提示',
                text: msg,
                styling: 'bootstrap3',
                delay: 5000,
                type: status == 2 ? 'info' : 'error'
            };
            new PNotify(obj);
        })
    },

    /**
     * 入金历史
     */
    history: function (type, direction) {
        //显示loading
        show_loading_table('moneyRechargeHistory_table', 5);
        var params = {};
        var pageNo_dom = 'moneyRechargeHistory_pageNo';
        var pageTotal_dom = 'moneyRechargeHistory_pageTotal';
        params.pageNo = getPageNo(pageNo_dom, pageTotal_dom, type, direction);

        var formObj = $('#form_moneyRechargeHistory');
        $.each(formObj.find('input,select'), function (i, v) {
            var name = $.trim($(v).attr('name'));
            if (name != '') {
                params[name] = $(v).val();
            }
        });
        $.getJSON('/money/recharge-history', params, function (json) {
            var t = json;
            var sumMoney = parseFloat(json.moneys[0]['moneys']);

            $("#" + pageNo_dom).val(parseInt(t.pageNo));
            $("#" + pageTotal_dom).html(parseInt(t.pageTotal));
            var tr_head = "<tr>" +
                "<td><i style='color: red;'>汇总</i></td>" +
                "<td></td>" +
                "<td></td>" +
                "<td class='text-right'>" +
                "<i>" + sumMoney+ "</i>" +
                "</td>" +
                "</tr>";
            var tr = "";
            $.each(t.lists, function (i, v) {
                var status;
                switch (parseInt(v.user_type)) {
                    case 1:
                        status = "<label class='label label-default'>审核</label>";
                        break;
                    case 2:
                        status = "<label class='label label-success'>成功</label>";
                        break;
                    case 3:
                        status = "<label class='label label-danger'>拒绝</label>";
                        break;
                    default:
                        status = "";
                        break;
                }
                tr += "<tr>";
                tr += "<td>" + convertDate(v.time, 1) + "</td>";
                tr += "<td class='text-center'>" + (v.money_order) + "</td>";
                tr += "<td>" + status + "</td>";
                tr +="<td>"+v.moneys+"</td>";
                tr += "</tr>";
            });

            $("#moneyRechargeHistory_table").find('tbody').html(tr_head + tr);

            page_content_onresize();
        })
    },

    /**
     * 支付详情
     * @param id
     */
    detail: function (id) {

    }
};